package com.an.trip.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import com.an.common.utils.poi.ExcelUtil;
import com.an.trip.domain.bo.TripDailyRecordBo;
import com.an.trip.domain.vo.TripDailyRecordVo;
import com.an.trip.service.ITripDailyRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 行程每日记录
 *
 * @author Ann
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/trip/dailyRecord")
public class TripDailyRecordController extends BaseController {

    private final ITripDailyRecordService tripDailyRecordService;

    /**
     * 查询行程每日记录列表
     */
    @SaCheckPermission("trip:dailyRecord:list")
    @GetMapping("/list")
    public TableDataInfo<TripDailyRecordVo> list(TripDailyRecordBo bo, PageQuery pageQuery) {
        return tripDailyRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出行程每日记录列表
     */
    @SaCheckPermission("trip:dailyRecord:export")
    @Log(title = "行程每日记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TripDailyRecordBo bo, HttpServletResponse response) {
        List<TripDailyRecordVo> list = tripDailyRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "行程每日记录", TripDailyRecordVo.class, response);
    }

    /**
     * 获取行程每日记录详细信息
     *
     * @param tripId 主键
     */
    @SaCheckPermission("trip:dailyRecord:query")
    @GetMapping("/{tripId}")
    public R<TripDailyRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long tripId) {
        return R.ok(tripDailyRecordService.queryById(tripId));
    }

    /**
     * 新增行程每日记录
     */
    @SaCheckPermission("trip:dailyRecord:add")
    @Log(title = "行程每日记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TripDailyRecordBo bo) {
        return toAjax(tripDailyRecordService.insertByBo(bo));
    }

    /**
     * 修改行程每日记录
     */
    @SaCheckPermission("trip:dailyRecord:edit")
    @Log(title = "行程每日记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TripDailyRecordBo bo) {
        return toAjax(tripDailyRecordService.updateByBo(bo));
    }

    /**
     * 结账
     *
     * @param tripIds 主键串
     */
    @SaCheckPermission("trip:dailyRecord:edit")
    @Log(title = "行程结账", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/settle/{tripIds}")
    public R<Void> settle(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tripIds) {
        return toAjax(tripDailyRecordService.settleByIds(Arrays.asList(tripIds)));
    }

    /**
     * 委托
     *
     * @param recordBo 记录 bo
     * @return {@link R }<{@link Void }>
     * @author ANH
     * @date 2025/01/03
     */
    @SaCheckPermission("trip:dailyRecord:delegate")
    @Log(title = "行程委派", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/delegate")
    public R<Void> delegate(@RequestBody TripDailyRecordBo recordBo) {
        return toAjax(tripDailyRecordService.delegate(recordBo));
    }

    /**
     * 查询被委派行程的原行程信息
     *
     * @param tripId 行程 ID
     * @return {@link R }<{@link TripDailyRecordVo }>
     * @author ANH
     * @date 2025/01/03
     */
    @SaCheckPermission("trip:dailyRecord:query")
    @GetMapping("/queryOriginalInfo/{tripId}")
    public R<TripDailyRecordVo> queryOriginalInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long tripId) {
        return R.ok(tripDailyRecordService.queryOriginalInfo(tripId));
    }

    /**
     * 查询委派行程信息
     *
     * @param tripId 行程 ID
     * @return {@link R }<{@link TripDailyRecordVo }>
     * @author ANH
     * @date 2025/01/03
     */
    @SaCheckPermission("trip:dailyRecord:query")
    @GetMapping("/queryDelegateInfo/{tripId}")
    public R<TripDailyRecordVo> queryDelegateInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long tripId) {
        return R.ok(tripDailyRecordService.queryDelegateInfo(tripId));
    }


    /**
     * 删除行程每日记录
     *
     * @param tripIds 主键串
     */
    @SaCheckPermission("trip:dailyRecord:remove")
    @Log(title = "行程每日记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tripIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tripIds) {
        return toAjax(tripDailyRecordService.deleteWithValidByIds(Arrays.asList(tripIds), true));
    }
}
